#include "head.h"

int main(int argc,char* argv[]){
    ARGS_CHECK(argc,3);
    int sockfd=socket(AF_INET,SOCK_STREAM,0);

    struct sockaddr_in addr;
    bzero(&addr,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_addr.s_addr=inet_addr(argv[1]);
    addr.sin_port=htons(atoi(argv[2]));

    int ret=connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect");
    
    login(sockfd);//登录判断

    char buf[4096];
    fd_set rdset;
    while(1){
        Command cmd;
        FD_ZERO(&rdset);
        FD_SET(STDIN_FILENO,&rdset);
        FD_SET(sockfd,&rdset);
        select(sockfd+1,&rdset,NULL,NULL,NULL);
        if(FD_ISSET(STDIN_FILENO,&rdset)){
            //todo:发送命令
            bzero(buf,sizeof(buf));
            bzero(&cmd,sizeof(cmd));

            read(STDIN_FILENO,buf,sizeof(buf));

            Which_Command(buf,&cmd);//分割命令
            Send_Command(sockfd,&cmd);//发送命令
            Do_Command(sockfd,&cmd);//执行命令
        }
        if(FD_ISSET(sockfd,&rdset)){
            Do_Command(sockfd,&cmd);//接收回复，执行命令
        }
    }
    close(sockfd);
    return 0;
}
